package com.hoshiibuy.sku.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.sku.entity.BaseSku;
import com.hoshiibuy.sku.entity.param.BaseSkuQueryParam;
import com.hoshiibuy.sku.entity.param.BaseSkuUpdateParam;
import com.hoshiibuy.sku.entity.param.BaseSkuAddParam;
import com.hoshiibuy.sku.service.BaseSkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.hoshiibuy.common.utils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 基础商品表SKU 控制器
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-05-05
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/sku/base-sku")
@Api(value = "SKU管理", tags = {"基础商品表SKU管理"})
public class BaseSkuController {
    @Autowired
    private BaseSkuService baseSkuService;

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 分页查询基础商品表SKU
    */
    @ApiOperation(value = "分页查询基础商品表SKU")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid BaseSkuQueryParam param) {
        try {
            log.info("分页查询基础商品表SKU");
            QueryWrapper<BaseSku> queryWrapper = new QueryWrapper<BaseSku>();
            queryWrapper.eq(param.getSpuid() != null, "spuid", param.getSpuid());
            queryWrapper.eq(StringUtils.isNotBlank(param.getSkuno()), "skuno", param.getSkuno());
            queryWrapper.eq(StringUtils.isNotBlank(param.getBarcode()), "barcode", param.getBarcode());
            queryWrapper.eq(StringUtils.isNotBlank(param.getPacketcode()), "packetcode", param.getPacketcode());
            queryWrapper.eq(param.getIsStored() != null, "is_stored", param.getIsStored());
            queryWrapper.eq(StringUtils.isNotBlank(param.getExtendProperty()), "extend_property", param.getExtendProperty());
            queryWrapper.eq(StringUtils.isNotBlank(param.getExtendSpec()), "extend_spec", param.getExtendSpec());
            queryWrapper.eq(StringUtils.isNotBlank(param.getSkuTitle()), "sku_title", param.getSkuTitle());
            queryWrapper.eq(StringUtils.isNotBlank(param.getStatus()), "status", param.getStatus());
            queryWrapper.eq(StringUtils.isNotBlank(param.getPacketUnit()), "packet_unit", param.getPacketUnit());
            queryWrapper.eq(param.getWeight() != null, "weight", param.getWeight());
            queryWrapper.eq(param.getDeleted() != null, "deleted", param.getDeleted());
            IPage<BaseSku> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            queryWrapper.select(BeanUtils.getSqlSelect(BaseSku.class));
            return Response.data(baseSkuService.page(page, queryWrapper));
          } catch (ServiceException e) {
            return Response.failed(e.getError().getMsg());
          } catch (Exception e) {
            log.error("分页查询基础商品表SKU异常", e);
            return Response.failed("分页查询基础商品表SKU异常");
        }
    }
    /**
    * @param id
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 根据id查询基础商品表SKU
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询基础商品表SKU")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(baseSkuService.getById(id));
    }

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 新增
    */
    @PostMapping("/save")
    @ApiOperation(value = "新增基础商品表SKU")
    public Response save(@RequestBody @Valid BaseSkuAddParam param) {
        try {
            BaseSku data = BeanUtils.covertType(param, BaseSku.class);
            return Response.data(baseSkuService.save(data));
        } catch (ServiceException e) {
            log.error("新增基础商品表SKU异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("新增基础商品表SKU异常", e);
            return Response.failed();
        }
    }

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 修改基础商品表SKU
    */
    @PutMapping("/update")
    @ApiOperation(value = "修改基础商品表SKU")
    public Response update(@RequestBody @Valid BaseSkuUpdateParam param) {
        try {
            BaseSku data = BeanUtils.covertType(param, BaseSku.class);
            return Response.data(baseSkuService.updateById(data));
        }catch (ServiceException e) {
            log.error("修改基础商品表SKU异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("修改基础商品表SKU异常", e);
            return Response.failed();
        }
    }
}
